﻿using SeeSharpTools.JY.ArrayUtility;
using SeeSharpTools.JY.DSP.Fundamental;
using SeeSharpTools.JY.GUI;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace SeeSharpTools.Demo.GUI
{
    public partial class FormChart : UIPage
    {
        private double[] sineWave = new double[10000];
        private int phase = 0;
        private double[] xValue = new double[10000];
        private double[] squareWave = new double[10000];
        private double[,] combineWave = new double[2, 10000];
        private double xStart = 0;
        private bool plot = false;

        public FormChart()
        {
            InitializeComponent();

            comboBox_zoomType.SelectedIndex = 0;
            checkBox_autoScaleY.Checked = true;
            comboBox_majorOrder.SelectedIndex = 0;
        }

        private void easyButton_plot_Click(object sender, EventArgs e)
        {
            easyChartX1.Series[0].Width = EasyChartXSeries.LineWidth.Middle;
            easyChartX1.Series[0].Type = EasyChartXSeries.LineType.Line;
            easyChartX1.Series[0].Marker = EasyChartXSeries.MarkerType.Star5;
            easyChartX1.Series[1].Width = EasyChartXSeries.LineWidth.Middle;
            easyChartX1.Series[1].Type = EasyChartXSeries.LineType.Line;
            easyChartX1.Series[1].Marker = EasyChartXSeries.MarkerType.Diamond;

            easyChartX1.AxisY.Maximum = (double)numericUpDown_chartYmax.Value;
            easyChartX1.AxisY.Minimum = (double)numericUpDown_chartYmin.Value;
            easyChartX1.AxisY.AutoScale = checkBox_autoScaleY.Checked;

            phase = 0;
            checkBox_series1.Checked = true;
            checkBox_series2.Checked = true;

            for (int i = 0; i < sineWave.Length; i++)
            {
                xValue[i] = 1 + i;
            }
            timer1.Enabled = true;
        }

        private void checkBox_split_CheckedChanged(object sender, EventArgs e)
        {
            easyChartX1.SplitView = checkBox_split.Checked;
        }

        private void checkBox_easyChartXlegendVisible_CheckedChanged(object sender, EventArgs e)
        {
            easyChartX1.LegendVisible = checkBox_easyChartXlegendVisible.Checked;
        }

        private void checkBox_showValue_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_showValue.Checked == true)
            {
                easyChartX1.XCursor.Mode = EasyChartXCursor.CursorMode.Cursor;

                easyChartX1.YCursor.Mode = EasyChartXCursor.CursorMode.Cursor;
            }
            else
            {
                switch (comboBox_zoomType.SelectedIndex)
                {
                    case 0:
                        easyChartX1.XCursor.Mode = EasyChartXCursor.CursorMode.Zoom;
                        easyChartX1.YCursor.Mode = EasyChartXCursor.CursorMode.Disabled;
                        break;

                    case 1:
                        easyChartX1.XCursor.Mode = EasyChartXCursor.CursorMode.Disabled;
                        easyChartX1.YCursor.Mode = EasyChartXCursor.CursorMode.Zoom;
                        break;

                    case 2:
                        easyChartX1.XCursor.Mode = EasyChartXCursor.CursorMode.Zoom;
                        easyChartX1.YCursor.Mode = EasyChartXCursor.CursorMode.Zoom;
                        break;

                    default:
                        break;
                }
            }
        }

        private void checkBox_autoScaleY_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_autoScaleY.Checked == true)
            {
                checkBox_customYrange.Checked = false;
                easyChartX1.AxisY.AutoScale = checkBox_autoScaleY.Checked;
            }
            else
            {
                easyChartX1.AxisY.AutoScale = checkBox_autoScaleY.Checked;
            }
        }

        private void checkBox_customYrange_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_customYrange.Checked == true)
            {
                checkBox_autoScaleY.Checked = false;
                easyChartX1.AxisY.Maximum = (double)numericUpDown_chartYmax.Value;
                easyChartX1.AxisY.Minimum = (double)numericUpDown_chartYmin.Value;
            }
        }

        private void checkBox_series1_CheckedChanged(object sender, EventArgs e)
        {
            easyChartX1.Series[0].Visible = checkBox_series1.Checked;
        }

        private void checkBox_series2_CheckedChanged(object sender, EventArgs e)
        {
            easyChartX1.Series[1].Visible = checkBox_series2.Checked;
        }

        private void button_saveAsCSV_Click(object sender, EventArgs e)
        {
            easyChartX1.SaveAsCsv();
        }

        private void button_saveAsImage_Click(object sender, EventArgs e)
        {
            easyChartX1.SaveAsImage();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;

            Generation.SineWave(ref sineWave, 0.5, phase, 5);
            Generation.SquareWave(ref squareWave, 0.5, 50, 5);
            ArrayManipulation.ReplaceArraySubset(sineWave, ref combineWave, 0, SeeSharpTools.JY.ArrayUtility.MajorOrder.Row);
            ArrayManipulation.ReplaceArraySubset(squareWave, ref combineWave, 1, SeeSharpTools.JY.ArrayUtility.MajorOrder.Row);
            for (int i = 0; i < combineWave.GetLength(0); i++)
            {
                for (int j = 0; j < combineWave.GetLength(1); j++)
                {
                    combineWave[i, j] = combineWave[i, j] + 1.5;
                }
            }
            xStart = 1;
            easyChartX1.Plot(combineWave, xStart, 1);
            phase++;
        }

        private void easyChartX1_TabCursorChanged(object sender, TabCursorEventArgs e)
        {
            if (easyChartX1.TabCursors.Count >= 2)
            {
                textBox_tabCursorSpacing.Text = (easyChartX1.TabCursors[0].Value - easyChartX1.TabCursors[1].Value).ToString("F3");
            }
        }

        private void button_plot2D_Click(object sender, EventArgs e)
        {
            double[,] data = new double[3, 8];
            for (int i = 0; i < data.GetLength(0); i++)
            {
                for (int j = 0; j < data.GetLength(1); j++)
                {
                    data[i, j] = i + j;
                }
            }
            if (comboBox_majorOrder.SelectedIndex == 0)
            {
                easyChartX2.Plot(data, 0, 1, SeeSharpTools.JY.GUI.MajorOrder.Row);
            }
            else
            {
                easyChartX2.Plot(data, 0, 1, SeeSharpTools.JY.GUI.MajorOrder.Column);
            }
        }

        private void button_marker_Click(object sender, EventArgs e)
        {
            List<double> x = new List<double>() { (double)numericUpDown_markerX.Value };
            List<double> y = new List<double>() { (double)numericUpDown_markerY.Value };
            easyChartX1.AddDataMarker(x, y, Color.Cyan);
        }

        private void button_markerClear_Click(object sender, EventArgs e)
        {
            easyChartX1.ClearMarker();
        }

        private void checkBox_Log_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_LogX.Checked == true)
            {
                easyChartX1.AxisX.IsLogarithmic = true;
            }
            else
            {
                easyChartX1.AxisX.IsLogarithmic = false;
            }
        }

        private void checkBox_YLogCoordinate_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_YLogCoordinate.Checked == true)
            {
                easyChartX1.AxisY.IsLogarithmic = true;
            }
            else
            {
                easyChartX1.AxisY.IsLogarithmic = false;
            }
        }

        private void button_clear_Click(object sender, EventArgs e)
        {
            easyChartX1.Clear();
        }

        private void easyChartX1_AfterPlot(object sender, EasyChartXPlotEventArgs e)
        {
            if (plot == true)
            {
                MessageBox.Show("绘图完毕,触发了AfterPlot事件");
                plot = false;
            }
        }

        private void easyChartX1_AxisViewChanged(object sender, EasyChartXViewEventArgs e)
        {
            double leftIndex = easyChartX1.AxisX.ViewMinimum;
            double rightIndex = easyChartX1.AxisX.ViewMaximum;
            if (leftIndex < this.easyChartX1.AxisX.Minimum)
            {
                leftIndex = this.easyChartX1.AxisX.Minimum;
            }
            if (rightIndex > this.easyChartX1.AxisX.Maximum)
            {
                rightIndex = this.easyChartX1.AxisX.Maximum;
            }
            double[,] viewData = new double[2, (int)(rightIndex - leftIndex)];
            for (int i = 0; i < viewData.GetLength(1); i++)
            {
                viewData[0, i] = combineWave[0, (int)(i + leftIndex - xStart)];
                viewData[1, i] = combineWave[1, (int)(i + leftIndex - xStart)];
            }
            easyChartX2.Plot(viewData);
        }

        private void easyChartX1_BeforePlot(object sender, EasyChartXPlotEventArgs e)
        {
            if (plot == false)
            {
                MessageBox.Show("即将开始绘图，触发了BeforePlot事件");
                plot = true;
            }
        }

        private void easyChartX1_TabCursorChanged_1(object sender, TabCursorEventArgs e)
        {
            if (easyChartX1.TabCursors.Count >= 2)
            {
                textBox_tabCursorSpacing.Text = (easyChartX1.TabCursors[0].Value - easyChartX1.TabCursors[1].Value).ToString("F3");
            }
        }

        private void comboBox_zoomType_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (comboBox_zoomType.SelectedIndex)
            {
                case 0:
                    easyChartX1.XCursor.Mode = EasyChartXCursor.CursorMode.Zoom;
                    easyChartX1.YCursor.Mode = EasyChartXCursor.CursorMode.Disabled;
                    break;

                case 1:
                    easyChartX1.XCursor.Mode = EasyChartXCursor.CursorMode.Disabled;
                    easyChartX1.YCursor.Mode = EasyChartXCursor.CursorMode.Zoom;
                    break;

                case 2:
                    easyChartX1.XCursor.Mode = EasyChartXCursor.CursorMode.Zoom;
                    easyChartX1.YCursor.Mode = EasyChartXCursor.CursorMode.Zoom;
                    break;

                default:
                    break;
            }
        }
    }
}